package com.lk.theurlpro.controller;

import com.lk.theurlpro.service.FileService;
import com.lk.theurlpro.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author lk
 * @date 2020/2/10 0010 11:07
 */
@Controller
public class FileController {

    @Autowired
    FileService fileService;

    // 文件上传，并读取
    @ResponseBody
    @RequestMapping(value = "/fileUpload")
    public String upload(@RequestParam("file") MultipartFile file) {    //注意参数
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));// 获取文件的后缀名
        String result = null;
        if (suffixName.equals(".txt")) {
            result = fileService.fileUpload(file);
        } else if (suffixName.equals(".html")) {
            result = fileService.htmlUpload(FileUtil.multipartFileToFile(file));
        }
        return result;
    }

}
